<?php

// 依赖文件的目录添加到这个数组
$prefixList = [
    "api\\lib",
    "api\\agent",
    "api\\request",
    "api\\response",
    "model\\repo",
    "model\\service"
];

foreach ($prefixList as $prefix) {
    spl_autoload_register(function ($clazz) use ($prefix) {
        $len = strlen($prefix);
        if (0 != strncmp($prefix, $clazz, $len)) {
            return;
        }
        $baseDir = dirname(__FILE__) . DIRECTORY_SEPARATOR .
            str_replace('\\', DIRECTORY_SEPARATOR, $prefix);
        $relativeClass = substr($clazz, $len);

        $file = $baseDir . str_replace('\\', DIRECTORY_SEPARATOR, $relativeClass) . '.php';
        if (!file_exists($file)) {
            throw new \InvalidArgumentException($file . ' does not exists');
        }
        require $file;
    });
}
